﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace TushuGuan_GUI
{
    class Program
    {
        const int length_screen = 50;
        const string chars = "-";
        static Management management = new Management();

        static void Main(string[] args)
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine();
                Tools.RepeatOutput(chars, length_screen, true);
                Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
                Console.WriteLine();
                Tools.CenterOutput("Powered By LittleFish", length_screen, 1, true);
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
                Tools.OutputOption(new string[] { "管理图书", "管理学生", "借阅管理", "退出" }, length_screen, 3, new double[] { 2, 2, 2, 2 }, true);
                int[] input = Tools.InputInt(1, new int[] { 1 }, new int[] { 4 });
                switch (input[0])
                {
                    case 1:
                        ManageBook();
                        break;
                    case 2:
                        ManageStudent();
                        break;
                    case 3:
                        ManageLend();
                        break;
                    case 4:
                        Guanyu();
                        return;
                }
            }
        }
        static void Guanyu()
        {
            Console.Clear();
            for (int i=0;i<8;i++)
            {
                Console.WriteLine();
            }
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("Powered By LittleFish", length_screen, 1, true);
            Console.WriteLine();
            Tools.CenterOutput("2021/2/1 11:48", length_screen, 1, true);
            Thread.Sleep(5000);
        }
        static void ManageLend()
        {
            while (true)
            {
                Console.Clear();

                Console.WriteLine();
                Tools.RepeatOutput(chars, length_screen, true);
                Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
                Console.WriteLine();
                Tools.CenterOutput("借阅管理", length_screen, 1, true);
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
                Tools.OutputOption(new string[] {  "借书", "还书","查看所有借书记录", "查看逾期未还名单", "返回" }, length_screen, 2, new double[] { 2, 2, 2, 2, 2 }, true);
                int[] input = Tools.InputInt(1, new int[] { 1 }, new int[] { 5 });
                switch (input[0])
                {
                    case 1:
                        LendBook();
                        break;
                    case 2:
                        ReturnBook();
                        break;
                    case 3:
                        LookAllLendBooks();
                        break;
                    case 4:
                        LookAllOverdueLendBooks();
                        break;
                    case 5:
                        return;
                }
            }
        }
        static void LookAllLendBook_table(List<BookBorrowedPlus> bookBorroweds)
        {
            string[,] chart = new string[bookBorroweds.Count + 1, 6];
            double[,] parameters = new double[bookBorroweds.Count + 1, 6];
            parameters[0, 0] = 2;
            parameters[0, 1] = 1.4;
            parameters[0, 2] = 2;
            parameters[0, 3] = 2;
            parameters[0, 4] = 2;
            parameters[0, 5] = 2;
            chart[0, 0] = "序号";
            chart[0, 1] = "书籍ISBN";
            chart[0, 2] = "借书学生";
            chart[0, 3] = "借书日期";
            chart[0, 4] = "借书天数限制";
            chart[0, 5] = "最迟还书日期";
            for (int i = 0; i < bookBorroweds.Count; i++)
            {
                chart[i + 1, 0] = (i + 1).ToString();
                chart[i + 1, 1] = bookBorroweds[i].ISBN;
                chart[i + 1, 2] = bookBorroweds[i].StudentId;
                chart[i + 1, 3] = bookBorroweds[i].Time.ToString("g");
                chart[i + 1, 4] = bookBorroweds[i].Lasttime.ToString();
                chart[i + 1, 5] = bookBorroweds[i].Time.AddDays(bookBorroweds[i].Lasttime).ToString("g");
                parameters[i + 1, 0] = 1;
                parameters[i + 1, 1] = 1;
                parameters[i + 1, 2] = 1;
                parameters[i + 1, 3] = 1;
                parameters[i + 1, 4] = 1;
                parameters[i + 1, 5] = 1;
            }

            Tools.OutputTable(chart, bookBorroweds.Count + 1, 6, parameters, true);
        }
        static void LookAllLendBooks()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查看所有借书记录", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            LookAllLendBook_table(management.AllLendBooks());
            Console.WriteLine();
            Console.WriteLine("按下回车返回");
            Console.ReadLine();
        }
        static void LookAllOverdueLendBooks()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查看逾期未还名单", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            LookAllLendBook_table(management.AllOverdueLendBooks());
            Console.WriteLine();
            Console.WriteLine("按下回车返回");
            Console.ReadLine();
        }
        static void ReturnBook()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("还书", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            while (true)
            {
                Console.WriteLine();

                Console.Write("请输入还书学生的学号：");
                List<Student> students = new List<Student>();
                while (students.Count != 1)
                {
                    string re_1 = Console.ReadLine();
                    if (re_1 == "")
                    {
                        return;
                    }
                    students = management.SearchStudents(re_1);
                    if (students.Count == 0)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);

                        Console.Write("学生不存在，请重新输入：");
                    }
                    if (students.Count > 1)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);

                        Console.WriteLine();
                        LookStudent_table(students);
                        Console.Write("无法确定目标学生，请重新输入：");
                    }
                    if (students[0].Lend.Count == 0)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);
                        Console.Write("该学生未借书，请重新输入：");
                        students.Clear();
                    }
                }
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
                LookLendBook_table(students[0].Lend);
                Console.Write("请输入所还书籍的序号：");

                int[] jieguo = Tools.InputInt(1, new int[] { 1 }, new int[] { students[0].Lend.Count });
                BookBorrowed book = students[0].Lend[jieguo[0]-1];
                try
                {
                    management.ReturnBook(book.ISBN, students[0].StudentID);
                    Console.WriteLine();
                    Tools.RepeatOutput(chars, length_screen, true);
                    Console.WriteLine("成功为" + students[0].StudentID + "归还" + book.ISBN);
                    Tools.RepeatOutput(chars, length_screen, true);
                }
                catch (Exception exc)
                {
                    Tools.RepeatOutput(chars, length_screen, true);
                    Console.WriteLine("为" + students[0].StudentID + "归还" + book.ISBN + " 图书失败");
                    Console.WriteLine("详细信息：" + exc.Message);
                    Tools.RepeatOutput(chars, length_screen, true);
                }


            }
        }
        static void LendBook()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("借书", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            while (true)
            {
                Console.WriteLine();
                Console.Write("请输入所借书籍的ISBN号：");

                List<Book> books = new List<Book>();
                while (books.Count != 1)
                {
                    string re_1 = Console.ReadLine();
                    if (re_1 == "")
                    {
                        return;
                    }
                    books = management.SearchBooks(re_1);
                    if (books.Count == 0)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);

                        Console.Write("书籍不存在，请重新输入：");
                    }
                    if (books.Count > 1)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);

                        Console.WriteLine();
                        LookBook_table(books);
                        Console.Write("无法确定目标书籍，请重新输入：");
                    }
                }

                Console.Write("请输入借书学生的学号：");
                List<Student> students = new List<Student>();
                while (students.Count != 1)
                {
                    string re_1 = Console.ReadLine();
                    if (re_1 == "")
                    {
                        return;
                    }
                    students = management.SearchStudents(re_1);
                    if (students.Count == 0)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);
                        Console.Write("学生不存在，请重新输入：");
                    }
                    if (students.Count > 1)
                    {
                        Tools.RepeatOutput(chars, length_screen, true);
                        Console.WriteLine();
                        LookStudent_table(students);
                        Console.Write("无法确定目标学生，请重新输入：");
                    }
                }
                try
                {
                    management.LendBook(books[0].ISBN, students[0].StudentID, 30);
                    Console.WriteLine();
                    Tools.RepeatOutput(chars, length_screen, true);
                    Console.WriteLine("成功为" + students[0].StudentID + "借阅" + books[0].ISBN);
                    Tools.RepeatOutput(chars, length_screen, true);
                }
                catch(Exception exc)
                {
                    Tools.RepeatOutput(chars, length_screen, true);
                    Console.WriteLine("为" + students[0].StudentID + "借阅" + books[0].ISBN + " 图书失败");
                    Console.WriteLine("详细信息：" + exc.Message);
                    Tools.RepeatOutput(chars, length_screen, true);
                }
            }
        }
        static void ManageStudent()
        {
            while (true)
            {
                Console.Clear();

                Console.WriteLine();
                Tools.RepeatOutput(chars, length_screen, true);
                Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
                Console.WriteLine();
                Tools.CenterOutput("管理学生", length_screen, 2, true);
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
                Tools.OutputOption(new string[] { "查看学生列表", "删除学生", "添加学生", "查找学生", "返回" }, length_screen, 3, new double[] { 2, 2, 2, 2, 2 }, true);
                int[] input = Tools.InputInt(1, new int[] { 1 }, new int[] { 5 });
                switch (input[0])
                {
                    case 1:
                        LookStudent();
                        break;
                    case 2:
                        DeleteStudent();
                        break;
                    case 3:
                        AddStudent();
                        break;
                    case 4:
                        SearchStudent();
                        break;
                    case 5:
                        return;
                }
            }

        }
        static void SearchStudent()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查找学生", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            while (true)
            {
                Console.WriteLine("请输入你想要查找的学生的学号（可以输入不完全）");
                Console.WriteLine("直接按下回车返回");
                string linshi = Console.ReadLine();
                if (linshi == "")
                {
                    return;
                }
                Tools.RepeatOutput(chars, length_screen, true);
                LookStudent_table(management.SearchStudents(linshi));
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
            }
        }
        static void DeleteStudent()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("删除学生", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            LookStudent_table(management.Students.Students);
            Console.WriteLine();
            Console.WriteLine("请输入你想要删除的学生序号（用空格分开多个序号或用-连接前后序号，可以批量删除）");
            Console.WriteLine("例如：4 5 8-11 12-14");
            Console.WriteLine("将会删除第4、5、8、9、10、11、12、13、14本书");
            string result = Console.ReadLine();
            if(result=="")
            {
                return;
            }
            List<int> result_ = Tools.InputIntBatch(result);
            bool if_success = management.RemoveStudents(result_);

            Tools.RepeatOutput(chars, length_screen, true);
            if (if_success == false)
            {
                Console.WriteLine("在删除过程中发生一个或多个错误，有可能部分学生删除成功，请以图书列表显示结果为准");
            }
            else
            {
                Console.WriteLine("成功删除所选学生");
            }
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            Console.WriteLine("按下回车返回");
            Console.ReadLine();

        }
        static void AddStudent()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("添加学生 直接按下回车返回上级菜单", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            while (true)
            {
                Console.WriteLine("正在添加第" + (management.Students.Students.Count + 1) + "位学生：");
                Console.Write("学生的学号：");
                string studentId = Console.ReadLine();
                if (studentId == "")
                {
                    return;
                }
                management.AddStudent(studentId);
                Console.WriteLine("成功添加第" + management.Students.Students.Count + "位学生");
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
            }
        }
        static void LookStudent_table(List<Student> students)
        {
            string[,] chart = new string[students.Count + 1, 3];
            double[,] parameters = new double[students.Count + 1, 3];
            parameters[0, 0] = 2;
            parameters[0, 1] = 2;
            parameters[0, 2] = 2;
            chart[0, 0] = "序号";
            chart[0, 1] = "学号";
            chart[0, 2] = "正在借的书籍数";
            for (int i = 0; i < students.Count; i++)
            {
                chart[i + 1, 0] = (i + 1).ToString();
                chart[i + 1, 1] = students[i].StudentID;
                chart[i + 1, 2] = students[i].Lend.Count.ToString();
                parameters[i + 1, 0] = 1;
                parameters[i + 1, 1] = 1;
                parameters[i + 1, 2] = 1;
            }

            Tools.OutputTable(chart, students.Count + 1, 3, parameters, true);
        }
        static void LookStudent()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查看学生列表", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();

            LookStudent_table(management.Students.Students);

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine("输入要查看详细信息的学生序号，或按下回车返回");
            string result= Console.ReadLine();
            int index = 0;
            try
            {
                index = int.Parse(result) - 1;
                StudentDetail(management.Students.Students[index]);
            }
            catch
            {

            }
        }
        static void StudentDetail(Student student)
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查看学生详细信息", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            Console.WriteLine("学号：" + student.StudentID);
            Console.WriteLine();

            Console.WriteLine("借书清单：");
            Console.WriteLine();

            LookLendBook_table(student.Lend);

            Console.WriteLine();
            Console.WriteLine("按下回车返回");
            Console.ReadLine();
        }
        static void LookLendBook_table(List<BookBorrowed> bookBorroweds)
        {
            string[,] chart = new string[bookBorroweds.Count + 1, 5];
            double[,] parameters = new double[bookBorroweds.Count + 1, 5];
            parameters[0, 0] = 2;
            parameters[0, 1] = 1.4;
            parameters[0, 2] = 2;
            parameters[0, 3] = 2;
            parameters[0, 4] = 2;
            chart[0, 0] = "序号";
            chart[0, 1] = "书籍ISBN";
            chart[0, 2] = "借书日期";
            chart[0, 3] = "借书天数限制";
            chart[0, 4] = "最迟还书日期";
            for (int i = 0; i < bookBorroweds.Count; i++)
            {
                chart[i + 1, 0] = (i + 1).ToString();
                chart[i + 1, 1] = bookBorroweds[i].ISBN;
                chart[i + 1, 2] = bookBorroweds[i].Time.ToString("g");
                chart[i + 1, 3] = bookBorroweds[i].Lasttime.ToString() ;
                chart[i + 1, 4] = bookBorroweds[i].Time.AddDays(bookBorroweds[i].Lasttime).ToString("g");
                parameters[i + 1, 0] = 1;
                parameters[i + 1, 1] = 1;
                parameters[i + 1, 2] = 1;
                parameters[i + 1, 3] = 1;
                parameters[i + 1, 4] = 1;
            }

            Tools.OutputTable(chart, bookBorroweds.Count + 1, 5, parameters, true);
        }
        static void ManageBook()
        {
            while (true)
            {
                Console.Clear();

                Console.WriteLine();
                Tools.RepeatOutput(chars, length_screen, true);
                Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
                Console.WriteLine();
                Tools.CenterOutput("管理图书", length_screen, 2, true);
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
                Tools.OutputOption(new string[] { "查看书籍列表", "删除图书", "添加图书", "查找图书" ,"返回"}, length_screen, 3, new double[] { 2, 2, 2, 2,2 }, true);
                int[] input = Tools.InputInt(1, new int[] { 1 }, new int[] { 5 });
                switch (input[0])
                {
                    case 1:
                        LookBooks();
                        break;
                    case 2:
                        DeleteBooks();
                        break;
                    case 3:
                        AddBook();
                        break;
                    case 4:
                        SearchBook();
                        break;
                    case 5:
                        return;
                }
            }

        }
        static void SearchBook()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查找图书", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            while (true)
            {
                Console.WriteLine("请输入你想要查找的图书ISBN编号（可以输入不完全）");
                Console.WriteLine("直接按下回车返回");
                string linshi = Console.ReadLine();
                if (linshi == "")
                {
                    return;
                }
                Tools.RepeatOutput(chars, length_screen, true);
                LookBook_table(management.SearchBooks(linshi));
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
            }
        }
        static void DeleteBooks()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("删除图书", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            LookBook_table(management.Books.Books);
            Console.WriteLine();
            Console.WriteLine("请输入你想要删除的图书序号（用空格分开多个序号或用-连接前后序号，可以批量删除）");
            Console.WriteLine("例如：4 5 8-11 12-14");
            Console.WriteLine("将会删除第4、5、8、9、10、11、12、13、14本书");
            string result = Console.ReadLine();
            if (result == "")
            {
                return;
            }

            List<int> result_ = Tools.InputIntBatch(result);
            bool if_success= management.RemoveBook(result_);

            Tools.RepeatOutput(chars, length_screen, true);
            if (if_success==false)
            {
                Console.WriteLine("在删除过程中发生一个或多个错误，有可能部分图书删除成功，请以图书列表显示结果为准");
            }
            else
            {
                Console.WriteLine("成功删除所选图书");
            }
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            Console.WriteLine("按下回车返回");
            Console.ReadLine();

        }
        static void AddBook()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("添加图书 直接按下回车返回上级菜单", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();
            while (true)
            {
                Console.WriteLine("正在添加第" + (management.Books.Books.Count + 1) + "本书：");
                Console.Write("书籍的ISBN码：");
                string isbn = Console.ReadLine();
                if (isbn == "")
                {
                    return;
                }
                management.Addbook(isbn);
                Console.WriteLine("成功添加第" + management.Books.Books.Count + "本书");
                Tools.RepeatOutput(chars, length_screen, true);
                Console.WriteLine();
            }
        }
        static void LookBook_table( List<Book> books)
        {
            //行列式
            string[,] chart = new string[books.Count + 1, 3];
            double[,] parameters = new double[books.Count + 1, 3];
            parameters[0, 0] = 2;
            parameters[0, 1] = 1.4;
            parameters[0, 2] = 2;
            chart[0, 0] = "序号";
            chart[0, 1] = "书籍ISBN";
            chart[0, 2] = "正在借书的学生";
            for (int i = 0; i < books.Count; i++)
            {
                chart[i + 1, 0] = (i+1).ToString();
                chart[i + 1, 1] = books[i].ISBN;
                chart[i + 1, 2] = books[i].StudentID;
                parameters[i + 1, 0] = 1;
                parameters[i + 1, 1] = 1;
                parameters[i + 1, 2] = 1;
            }

            Tools.OutputTable(chart, books.Count + 1, 3, parameters, true);
        }
        static void LookBooks()
        {
            Console.Clear();

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Tools.CenterOutput("图书馆管理系统", length_screen, 2, true);
            Console.WriteLine();
            Tools.CenterOutput("查看书籍列表", length_screen, 2, true);
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine();

            LookBook_table(management.Books.Books);

            Console.WriteLine();
            Tools.RepeatOutput(chars, length_screen, true);
            Console.WriteLine("按下回车返回");
            Console.ReadLine();
        }
    }
}
